package com.witframework.components.tag;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.witframework.WitException;

/**
 * @author wuxinyang
 * Jun 4, 2009 9:37:20 AM
 * 
 */
public class DataSetAuthTag extends BodyTagSupport{
	
	private String data;//数据源，可以是实体的类名或数据库的表名，或是sql查询语句
	private String data2;
	private String properties;//在列表中显示的字段
	private String category;//实体分类
	private String entryid;//权限项标识
	private String entryname;//权限项名称
	private String title="";//对话框的标题
	/**
	 * 数据源类型,table:数据库表；class:实体类名(通过对应的service查找数据),默认的数据源类型；
	 * sql:sql 查询语句.如果为sql类型，则filters属性的设置是无效的
	 */
	private String datatype="class";
	private String datastyle="table";
	private String root;
	private String parentid;//指向父节点的属性名称，对于PAGE和SQL类型则忽略该属性	
	private String parentid2;//指向父节点的属性名称，对于PAGE和SQL类型则忽略该属性	
	private String nodeid;//对应的熟悉名称(树节点的ID)，对于PAGE类型则忽略该属性
	private String nodeid2;	
	private String nodelabel;//上面对应的数据字段名称(树节点的label文本)，对于PAGE类型则忽略该属性
	private String nodelabel2;	
	private String titles;//列表的字段标题,如："name|实体名称,comment|说明,catalog|分类"，对于PAGE类型则忽略该属性
	private String multiple="0";//是否允许多选
	private String onlyLeaf="1";
	private String width;//对话框宽度
	private String height;//对话框高度	
	
	private String target;
	private String target_value;
	
	
	@Override
	public int doAfterBody() throws JspException {		
		if((datatype==null||datatype.equalsIgnoreCase("table")||datatype.equalsIgnoreCase("sql"))
			&&(category==null)){
			throw new WitException("非page类型的数据源时,必须设置properties和category属性");
		}
		BodyContent body = getBodyContent();
	    String value = body.getString();
	    body.clearBody();
	    if (value == null || value.length() == 0)
            return SKIP_BODY;
		String dataUrl="/dataSetAuthController.wit?";//数据列表的URL		
		
		HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();		
		if(dataUrl.startsWith("/")&&!dataUrl.startsWith(request.getContextPath())) dataUrl=request.getContextPath()+dataUrl;
		StringBuffer html=new StringBuffer(dataUrl);		
		try {
			html.append("data=").append(java.net.URLEncoder.encode(data,"utf-8"));
		} catch (UnsupportedEncodingException e1) {
			html.append("data=").append(data);
		}
		html.append("&datatype=").append(datatype);		
		html.append("&properties=").append(properties);
		html.append("&category=").append(category);
		html.append("&entryid=").append(entryid);
		html.append("&entryname=").append(entryname);
		if(StringUtils.isNotEmpty(datastyle)) html.append("&datastyle=").append(datastyle);
		if(StringUtils.isNotEmpty(title)) html.append("&title=").append(title);
		if(StringUtils.isNotEmpty(multiple)) html.append("&multiple=").append(multiple);
		if(StringUtils.isNotEmpty(height)) html.append("&height=").append(height);
		if(StringUtils.isNotEmpty(width)) html.append("&width=").append(width);
		if(StringUtils.isNotEmpty(data2)) html.append("&data2=").append(data2);
		if(StringUtils.isNotEmpty(root)) html.append("&root=").append(root);
		if(StringUtils.isNotEmpty(parentid)) html.append("&parentid=").append(parentid);
		if(StringUtils.isNotEmpty(parentid2)) html.append("&parentid2=").append(parentid2);
		if(StringUtils.isNotEmpty(nodeid)) html.append("&nodeid=").append(nodeid);
		if(StringUtils.isNotEmpty(nodeid2)) html.append("&nodeid2=").append(nodeid2);
		if(StringUtils.isNotEmpty(nodelabel)) html.append("&nodelabel=").append(nodelabel);
		if(StringUtils.isNotEmpty(nodelabel2)) html.append("&nodelabel2=").append(nodelabel2);
		if(StringUtils.isNotEmpty(onlyLeaf)) html.append("&onlyLeaf=").append(onlyLeaf);
		if(StringUtils.isNotEmpty(target)) html.append("&target=").append(target);
		if(StringUtils.isNotEmpty(target_value)) html.append("&target_value=").append(target_value);
		if(StringUtils.isNotEmpty(titles))
			try {
				html.append("&titles=").append(java.net.URLEncoder.encode(titles,"utf-8"));
			} catch (UnsupportedEncodingException e1) {
				html.append("&titles=").append(titles);
			}
		try {			
			getPreviousOut().print("<a href=\""+html.toString()+"\">"+value+"</a>");
		} catch (IOException e) {
			throw new JspException("Print/Flush in LoadingTag tag error:" + e);
		}
		 return SKIP_BODY;
	}
	public void setData2(String data2) {
		this.data2 = data2;
		try {
			this.data2=(String) ExpressionEvaluatorManager.evaluate("data2",data2,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setParentid(String parentid) {
		this.parentid = parentid;
		try {
			this.parentid=(String) ExpressionEvaluatorManager.evaluate("parentid",parentid,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setNodeid(String nodeid) {
		this.nodeid = nodeid;
		try {
			this.nodeid=(String) ExpressionEvaluatorManager.evaluate("nodeid",nodeid,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setNodeid2(String nodeid2) {
		this.nodeid2 = nodeid2;
		try {
			this.nodeid2=(String) ExpressionEvaluatorManager.evaluate("nodeid2",nodeid2,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setNodelabel(String nodelabel) {
		this.nodelabel = nodelabel;
		try {
			this.nodelabel=(String) ExpressionEvaluatorManager.evaluate("nodelabel",nodelabel,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setNodelabel2(String nodelabel2) {
		this.nodelabel2 = nodelabel2;
		try {
			this.nodelabel2=(String) ExpressionEvaluatorManager.evaluate("nodelabel2",nodelabel2,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setOnlyLeaf(String onlyLeaf) {
		this.onlyLeaf = onlyLeaf;
		try {
			this.onlyLeaf=(String) ExpressionEvaluatorManager.evaluate("onlyLeaf",onlyLeaf,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}
	public void setData(String data) {
		this.data = data;
		try {
			this.data=(String) ExpressionEvaluatorManager.evaluate("data",data,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setProperties(String properties) {
		this.properties = properties;
		try {
			this.properties=(String) ExpressionEvaluatorManager.evaluate("properties",properties,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setCategory(String category) {
		this.category = category;
		try {
			this.category=(String) ExpressionEvaluatorManager.evaluate("category",category,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setEntryid(String entryid) {
		this.entryid = entryid;
		try {
			this.entryid=(String) ExpressionEvaluatorManager.evaluate("entryid",entryid,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setEntryname(String entryname) {
		this.entryname = entryname;
		try {
			this.entryname=(String) ExpressionEvaluatorManager.evaluate("entryname",entryname,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setTitle(String title) {
		this.title = title;
		try {
			this.title=(String) ExpressionEvaluatorManager.evaluate("title",title,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setDatatype(String datatype) {
		this.datatype = datatype;
		try {
			this.datatype=(String) ExpressionEvaluatorManager.evaluate("datatype",datatype,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setDatastyle(String datastyle) {
		this.datastyle = datastyle;
		try {
			this.datastyle=(String) ExpressionEvaluatorManager.evaluate("datastyle",datastyle,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setTitles(String titles) {
		this.titles = titles;
		try {
			this.titles=(String) ExpressionEvaluatorManager.evaluate("titles",titles,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setMultiple(String multiple) {
		this.multiple = multiple;
		try {
			this.multiple=(String) ExpressionEvaluatorManager.evaluate("multiple",multiple,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setRoot(String root) {
		this.root = root;
		try {
			this.root=(String) ExpressionEvaluatorManager.evaluate("root",root,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}
	public void setWidth(String width) {
		this.width = width;
		try {
			this.width=(String) ExpressionEvaluatorManager.evaluate("width",width,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
		
	}
	public void setHeight(String height) {
		this.height = height;
		try {
			this.height=(String) ExpressionEvaluatorManager.evaluate("height",height,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}
	public void setParentid2(String parentid2) {
		this.parentid2 = parentid2;
	}
	public void setTarget(String target) {
		this.target = target;
		try {
			this.target=(String) ExpressionEvaluatorManager.evaluate("target",target,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}
	public void setTarget_value(String target_value) {
		this.target_value = target_value;
		try {
			this.target_value=(String) ExpressionEvaluatorManager.evaluate("target_value",target_value,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}
}
